<!--  -->
<template>
  <div class="inventory-container">
    <!-- 搜索区域 -->
    <div class="search-section">
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="商品名称">
          <el-input
            v-model="searchForm.productName"
            placeholder="请输入商品名称"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格区域 -->
    <el-table
      v-loading="loading"
      :data="inventoryList"
      border
      stripe
    >
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column prop="productName" label="商品名称" min-width="150" />
      <el-table-column prop="imageUrl" label="商品图片" width="100">
        <template slot-scope="scope">
          <el-image
            :src="scope.row.imageUrl"
            :preview-src-list="[scope.row.imageUrl]"
            fit="cover"
            style="width: 50px; height: 50px"
          >
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="categoryName" label="商品分类" min-width="120" />
      <el-table-column prop="supplierName" label="供应商" min-width="120" />
      <el-table-column prop="stock" label="库存数量" width="100">
        <template slot-scope="scope">
          <span :class="{ 'low-stock': scope.row.stock < 10 }">
            {{ scope.row.stock }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="销售价" width="120">
        <template slot-scope="scope">
          ¥{{ scope.row.price }}
        </template>
      </el-table-column>
      <el-table-column prop="purchasePrice" label="采购价" width="120">
        <template slot-scope="scope">
          ¥{{ scope.row.purchasePrice }}
        </template>
      </el-table-column>
      <el-table-column 
        prop="updatedAt" 
        label="最后更新时间" 
        width="180"
        :formatter="(row) => formatDate(row.updatedAt)"
      />
      <el-table-column label="状态" width="100">
        <template slot-scope="scope">
          <el-tag :type="getStockStatusType(scope.row.stock)">
            {{ getStockStatusText(scope.row.stock) }}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-section">
      <el-pagination
        :current-page.sync="pageNum"
        :page-size="pageSize"
        :total="total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { selectAllInventory } from '@/api/inventory'
import { formatDate } from '@/utils/date'

export default {
  name: 'Inventory',
  data() {
    return {
      loading: false,
      searchForm: {
        productName: ''
      },
      inventoryList: [],
      pageNum: 1,
      pageSize: 10,
      total: 0
    }
  },
  created() {
    this.getList()
  },
  methods: {
    formatDate,
    async getList() {
      this.loading = true
      try {
        const res = await selectAllInventory(
          this.pageNum,
          this.searchForm.productName
        )
        if (res.code === 200) {
          this.inventoryList = res.data.list || []
          this.total = res.data.total || 0
        }
      } catch (error) {
        console.error('获取库存列表失败:', error)
        this.$message.error('获取库存列表失败')
      } finally {
        this.loading = false
      }
    },
    getStockStatusType(stock) {
      if (stock <= 0) return 'danger'
      if (stock < 10) return 'warning'
      return 'success'
    },
    getStockStatusText(stock) {
      if (stock <= 0) return '缺货'
      if (stock < 10) return '低库存'
      return '正常'
    },
    handleSearch() {
      this.pageNum = 1
      this.getList()
    },
    resetSearch() {
      this.searchForm = {
        productName: ''
      }
      this.handleSearch()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.getList()
    }
  }
}
</script>

<style lang="less" scoped>
.inventory-container {
  padding: 20px;

  .search-section {
    background: #fff;
    padding: 18px 20px;
    border-radius: 4px;
    margin-bottom: 16px;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  }

  .pagination-section {
    margin-top: 16px;
    display: flex;
    justify-content: flex-end;
  }

  .low-stock {
    color: #E6A23C;
  }

  .image-slot {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    background: #f5f7fa;
    color: #909399;
    font-size: 20px;
  }
}
</style>
